#include "serialportcommunication.h"
#include "ui_serialportcommunication.h"
#include <QDebug>
#include "serialportmasterthread.h"
#include "serialportdialog.h"

SerialPortCommunication::SerialPortCommunication(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialPortCommunication)
{
    ui->setupUi(this);

    ui->spinBoxWaitResponse->setRange(0, 10000);
    ui->spinBoxWaitResponse->setValue(1000);

    ui->lineEditRequest->setText(tr("?"));

    ui->spinBoxTimePeriod->setRange(10, 5000);
    ui->spinBoxTimePeriod->setValue(1000);

    m_masterThread = new SerialPortMasterThread();

    connect(ui->pushButtonRunSampling, SIGNAL(clicked(bool)),
            this, SLOT(slotTransaction()));//开始采集
    connect(ui->pushButtonStopSampling, SIGNAL(clicked(bool)),
            this, SLOT(slotStopTransaction()));//暂停采集
    connect(ui->checkBoxAutoTransaction, SIGNAL(toggled(bool)),
            this, SLOT(slotAutoTransactionToggled(bool)));//自动采集选项

    connect(m_masterThread, SIGNAL(signalResponse(const QString)),
            this, SLOT(slotShowResponse(const QString)));
    connect(m_masterThread, SIGNAL(signalError(const QString)),
            this, SLOT(slotProcessError(const QString)));
    connect(m_masterThread, SIGNAL(signalTimeout(const QString)),
            this, SLOT(slotProcessTimeout(const QString)));
    connect(m_masterThread, SIGNAL(signalSerialPortConnected()),
            this, SLOT(slotSerialPortConnected()));

}

SerialPortCommunication::~SerialPortCommunication()
{
    delete ui;
//    delete m_masterThread;
}

void SerialPortCommunication::setSerialPortInstance(QSerialPort *portInstance)
{
    this->m_portInstance = portInstance;
    m_masterThread->setSerialPortInstance(portInstance);
}

void SerialPortCommunication::getSerialPortSettings(SerialPortDialog::SerialPortSettings settings)
{
    m_settings = settings;
    m_portName = settings.name;
    m_baudRate = settings.baudRate;
    m_dateBits = settings.dataBits;
    m_parity   = settings.parity;
    m_stopBits = settings.stopBits;
    m_flowControl = settings.flowControl;
}

void SerialPortCommunication::setControlsEnabled(bool enable)
{
    ui->pushButtonRunSampling->setEnabled(enable);
    ui->spinBoxWaitResponse->setEnabled(enable);
    ui->lineEditRequest->setEnabled(enable);
}

void SerialPortCommunication::slotTransaction()
{
    setControlsEnabled(false);
    m_request = ui->lineEditRequest->text();
    m_waitResponse = ui->spinBoxWaitResponse->value();
    ui->labelCurrentStatus->setText(tr("Running, connected to port %1.")
                                    .arg(m_portName));
    m_masterThread->transaction(m_portName, m_baudRate, m_dateBits, m_parity, m_stopBits, m_flowControl,
                                m_waitResponse,
                                m_request);
}

void SerialPortCommunication::slotStopTransaction()
{
    m_masterThread->stopTransaction();
}

void SerialPortCommunication::slotShowResponse(const QString &s)
{
    setControlsEnabled(true);
//    ui->labelTrafficStatus->setText(tr("transaction #%1:"
//                                       "\n\r-request: %2"
//                                       "\n\r-response: %3")
//                                    .arg(++m_transactionCount).arg(ui->lineEditRequest->text()).arg(s));
    ui->labelTrafficStatus->setText(tr("transaction #%1:")
                                    .arg(++m_transactionCount));
}

void SerialPortCommunication::slotProcessError(const QString &s)
{
    setControlsEnabled(true);
    ui->labelCurrentStatus->setText(tr("Not running, %1.").arg(s));
    ui->labelTrafficStatus->setText(tr("No traffic."));
}

void SerialPortCommunication::slotProcessTimeout(const QString &s)
{
    setControlsEnabled(true);
    ui->labelCurrentStatus->setText(tr("Running, %1.").arg(s));
    ui->labelTrafficStatus->setText(tr("No traffic."));
}

void SerialPortCommunication::slotSerialPortConnected()
{
    emit signalSerialPortConnected();
}

void SerialPortCommunication::slotAutoTransactionToggled(bool checked)
{
    m_masterThread->setAutoSampling(checked);
    m_masterThread->setSamplingPeriod(ui->spinBoxTimePeriod->value());
    if (checked)
    {
        qDebug() << "SerialPortCommunication: Turn on auto sampling.";
        qDebug() << "SerialPortCommunication: Auto sampling period = " << ui->spinBoxTimePeriod->value() << " .";
    }
    else
    {
        qDebug() << "SerialPortCommunication: Turn off auto sampling.";
    }
}
